/**
 * 
 */
package com.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.annotation.LoginNeedless;
import com.base.web.controller.BaseController;
import com.model.FormBean;
import com.model.User;
import com.web.ContactForm;

/**
 * @author leiruiqi
 *
 */
@LoginNeedless
@Controller
public class TestController extends BaseController{
	
	

	@RequestMapping(value="/test/testValidate")
	public String testValidate(ContactForm form,Model model){
		if(form.getContact()==null||form.getContact().getRealName()==null||form.getContact().getAddress()==null){
			return "redirect:/test/contactPage.do";
		}
		model.addAttribute("contact", form.getContact());
		return "/test/viewContact";
	}
	
	
	@RequestMapping(value="/test/contactPage")
	public String testValidate(HttpServletRequest request,HttpServletResponse response,ModelMap model){
	//	String path = request.getContextPath();  
	//	String base = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 

		
		return "/test/contact";
	}
	
	@RequestMapping(value="/test/testPage")
	public String testPage(String pageName,Model model){
		return "/test/"+pageName;
	}
	

	
	@RequestMapping(value="/id/{userid}/name/{username}")
	public String queryUser(@PathVariable("userid") long userID , @PathVariable("username") String userName, Map<String, User> model) {
       User user = new User();
       user.setUserId(userID);
       user.setUserName(userName);
       model.put("user", user);
       return "/test/home";
    }
	
	
	@RequestMapping(value="/test/testForm.do")   
	public String testFormBean(FormBean fromBean,ModelMap model){  
	    //model.addAttribute("formBean", new FormBean());  
	    return "/test/formbean";
	}  
	
	
	@Value("#{configProperties['test.abc']}")
	private int abc;
	
	
	@RequestMapping("/test/checkValue") 
	public @ResponseBody Map checkValue(){
		Map map = new HashMap();
		map.put("abc", abc);
		return map;
	}
	
	@RequestMapping("/test/userJsonValue") 
	public @ResponseBody User userJsonValue(){
		User user = new User();
		user.setUserId(12345L);
		user.setPassWord("12345");
		user.setUserName("ricky");
		return user;
	}
	
	@RequestMapping("/test/checkJson") 
	public @ResponseBody User checkJson(@RequestBody User user){
		return user;
	}
	
}
